package com.everyline.lib_base.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.everyline.lib_base.ext.getVmClazz
import com.everyline.lib_base.network.manager.NetState
import com.everyline.lib_base.network.manager.NetworkStateManager
import com.tbruyelle.rxpermissions3.RxPermissions

/**
 * Created by on 2021/2/24.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

abstract class BaseDBFragment<VM : BaseViewModel, DB : ViewDataBinding> : Fragment() {
    lateinit var mViewModel: VM
    lateinit var mViewBinding: DB

    //是否第一次加载
    private var isFirst: Boolean = true
    lateinit var mActivity: AppCompatActivity
    lateinit var mContext: Context
    val mRxPermissions: RxPermissions by lazy { RxPermissions(this) }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as AppCompatActivity
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewBinding = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false)

        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isFirst = true
        initView(savedInstanceState)
        initViewModel()
        registorDefUIChange()
        initData()
        createObserver()
        initListener()

    }

    override fun onResume() {
        super.onResume()
        onVisible()
    }

    fun registorDefUIChange() {
        mViewModel.loadingChange.showDialog.observe(viewLifecycleOwner, Observer {
            showLoading(
                if (it.isEmpty()) {
                    "请求网络中..."
                } else it
            )
        })
        mViewModel.loadingChange.dismissDialog.observe(viewLifecycleOwner, Observer {
            dismissLoading()
        })
    }

    abstract fun createObserver()

    fun onVisible() {
        if (lifecycle.currentState == Lifecycle.State.STARTED && isFirst) {
            lazyLoadData()
            //在Fragment中，只有懒加载过了才能开启网络变化监听
            NetworkStateManager.instance.mNetworkStateCallback.observe(
                viewLifecycleOwner,
                Observer {
                    //不是首次订阅时调用方法，防止数据第一次监听错误
                    if (!isFirst) {
                        onNetworkStateChanged(it)
                    }
                })
            isFirst = false
        }
    }

    abstract fun getLayoutResId(): Int
    abstract fun initView(savedInstanceState: Bundle?)
    abstract fun initData()
    abstract fun lazyLoadData()
    abstract fun initListener()

    open fun onNetworkStateChanged(it: NetState?) {}

    abstract fun showLoading(message: String = "请求网络中...")

    abstract fun dismissLoading()

    fun initViewModel() {
        mViewModel = ViewModelProvider(this).get(getVmClazz(this))
    }


}